package org.wkg.lesson11;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Demo_1101 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "申请资源...");
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "========申请到资源========");
                    TimeUnit.SECONDS.sleep(1);
                    semaphore.release();
                    System.out.println(Thread.currentThread().getName() + "******销毁资源******");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        for (int i = 0; i < 20; i++) {
            Thread t = new Thread(runnable);
            t.start();
        }
    }

}
